package com.youngjun.user.service.impl;

import com.youngjun.common.exception.CommonExceptionType;
import com.youngjun.common.oss.expection.CommonException;
import com.youngjun.user.mapper.UserMapper;
import com.youngjun.user.pojo.domain.Address;
import com.youngjun.user.pojo.domain.Department;
import com.youngjun.user.pojo.domain.Organization;
import com.youngjun.user.pojo.domain.User;
import com.youngjun.user.pojo.dto.UserDTO;
import com.youngjun.user.pojo.dto.UserRoleDTO;
import com.youngjun.user.pojo.dto.query.UserQueryDTO;
import com.youngjun.user.pojo.vo.AddressVO;
import com.youngjun.user.pojo.vo.DepartmentVO;
import com.youngjun.user.pojo.vo.RoleVO;
import com.youngjun.user.pojo.vo.UserVO;
import com.youngjun.user.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Calendar;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;

    @Autowired
    PasswordEncoder passwordEncoder;

    @Autowired
    RoleService roleService;

    @Autowired
    UserRoleService userRoleService;

    @Autowired
    AddressService addressService;

    @Autowired
    OrganizationService organizationService;

    @Autowired
    DepartmentService departmentService;

    @Override
    public UserMapper getMapper() {
        return userMapper;
    }

    /**
     * 转换信息
     * @param user 地址
     * @return UserVO
     */
    private UserVO transform(User user){
        UserVO vo = new UserVO().mapFrom(user);
        List<RoleVO> roleList = roleService.queryListByUserId(user.getId());
        vo.setRoleList(roleList);
        Organization organization = organizationService
                .select(new Organization().setId(user.getOrganizationId()));
        vo.setOrganizationName(organization.getName());
        if(user.getDepartmentId() != null){
            List<DepartmentVO> departmentList = departmentService.querySuperListById(user.getDepartmentId());
            vo.setDepartmentList(departmentList);
        }
        if(user.getAddressId() != null){
            AddressVO address = addressService.queryById(user.getAddressId());
            vo.setAddress(address);
        }
        return vo;
    }

    @Override
    public UserVO queryById(Long id) {
        User user = select(new User().setId(id));
        return transform(user);
    }

    @Override
    public List<UserVO> queryList(UserQueryDTO dto) {
        return selectAll(new User().mapFrom(dto))
                .stream()
                .map(this::transform)
                .collect(Collectors.toList());
    }

    @Override
    public UserVO create(UserDTO dto) {
        User user = new User().mapFrom(dto);
        user.setPassword(passwordEncoder.encode(dto.getPassword()));
        user.setIsEnabled(true);
        user.setIsAccountNonExpired(true);
        user.setIsAccountNonLocked(true);
        user.setIsCredentialsNonExpired(true);
        if(dto.getAddress() != null){
            AddressVO address = addressService.create(dto.getAddress());
            user.setAddressId(address.getId());
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.MONTH,6);
        user.setNextPasswordModifyTime(new Timestamp(calendar.getTimeInMillis()));
        insert(user);

        UserRoleDTO userRoleDto=new UserRoleDTO();
        userRoleDto.setUserId(user.getId());
        userRoleDto.setRoleIds(dto.getRoleIds());
        userRoleService.create(userRoleDto);
        return new UserVO().mapFrom(user);
    }

    @Override
    public UserVO modify(UserDTO dto) {
        User user = new User().mapFrom(dto);
        if(dto.getAddress() != null){
            AddressVO address = addressService.create(dto.getAddress());
            user.setAddressId(address.getId());
        }
        updateWithOptionalByPrimaryKey(user);

        UserRoleDTO userRoleDto=new UserRoleDTO();
        userRoleDto.setUserId(user.getId());
        userRoleDto.setRoleIds(dto.getRoleIds());
        userRoleService.create(userRoleDto);
        return new UserVO().mapFrom(user);
    }

    @Override
    public void delete(Long id) {
        deleteWithLogic(id);
        userRoleService.deleteByUserId(id);
    }

    @Override
    public List<UserVO> queryListByCurrentOrganization(UserQueryDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return queryList(dto);
    }

    @Override
    public UserVO createByCurrentOrganization(UserDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return create(dto);
    }

    @Override
    public UserVO modifyByCurrentOrganization(UserDTO dto) {
        dto.setOrganizationId(1459153169673232384L);
        return modify(dto);
    }

    @Override
    public void deleteByCurrentOrganization(Long id) {
        User user = new User();
        user.setOrganizationId(1459153169673232384L);
        user.setId(id);
        if(select(user) == null){
            throw new CommonException(CommonExceptionType.DB_DELETE_FAILED);
        }
        delete(id);
    }

}